package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import databeans.Customer;
import databeans.Employee;


import model.EmployeeDAO;
import model.Model;


public class EmployeeDashboardAction extends Action {
    private EmployeeDAO employeeDAO;

    public EmployeeDashboardAction(Model model) {
        employeeDAO = model.getEmployeeDAO();
    }

    public String getName() {
        return "employeeDashboard.do";
    }

    public String perform(HttpServletRequest request) {
        //ATTENTION
        // define target page
        String loginPage = "employee/login-form.jsp";
        String dashboardPage = "employee/employee-dashboard.jsp";
        String originPage = loginPage;
        String successPage = dashboardPage; //TODO change to employee dashboard
        
        System.out.println("Start perform");
        
        //set the employee
        Employee employee = null;
        Customer customer = null;

        
        //errors and messages will be stored here
        //resetting those containers
        List<String> errors = new ArrayList<String>();
        List<String> messages = new ArrayList<String>();
   
        //define the error and message
        request.setAttribute("errors", errors);
        request.setAttribute("messages", messages);
        
        //get all the parameter from the form
        String requestType = request.getParameter("Submit");
        //String username = request.getParameter("txtUsername");
        //String password = request.getParameter("txtPassword");
        

        //reload the form field
        //request.setAttribute("txtUsername", username);
        //request.setAttribute("txtPassword", password);
 
        
        //check if employee has been logged in
        //request.getSession() will return the current session and if one does not exist, a new session will be cretaed.
        //request.getSession(true) will return the current session if one exists, if one doesn't exits a new one will be created.
        //So there is actually no difference between the two methods HOWEVER, if you use request.getSession(false), it will return the current session if one exists and if one DOES NOT exist a new one will NOT be cretaed.
        employee = (Employee) request.getSession(true).getAttribute("employee");
        if (employee != null) {
            messages.add("Hi " + employee.getFirstName());
            //errors.add(employee.getFirstName() + ", you have already logged in under username " + employee.getUsername());
            return successPage ;
        } 
        

        
        //check if customer has actually logged in
        customer = (Customer) request.getSession(true).getAttribute("customer");
        if (customer != null) {
            errors.add("This section is closed for customer");
            return originPage;
        }
        

        
        //validate if user has necessary qualification
        //check the session

        // check for get or post request
        //if there is no parameter get passed, return to origin page
        /*
        if (requestType == null) {
            messages.add("To log in, enter your username and password");
            return originPage;     
        }
        */
        
        //validate the input
        //nothing to validate


        // at this stage, all validation should have been done
        try { 
            // Attach (this copy of) the employee bean to the session
            HttpSession session = request.getSession(true); 
            //employee = (Employee) request.getSession(true).getAttribute("employee");
            employee = (Employee) request.getSession(true).getAttribute("employee");
            
            if (employee!= null) {
                session.setAttribute("employee", employee);
                //set the welcome message
                messages.add("Hi " + employee.getFirstName() + "!, welcome to Carnegie Financial Service's iFund");
                messages.add("You have succesfully logged on as " + employee.getUsername());
                
                return successPage;
            } else {
                messages.add("Hi Guest!, welcome to Carnegie Financial Service's iFund");
                messages.add("Enter your login information below");
                

                return loginPage;
            }

        } catch (Exception e) {
            errors.add("Unable to validate email and password.");
            return originPage;
        }

    }
}